package com.ricchieri.manage.bean;

import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.context.RequestContext;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.UserFilter;
import com.ricchieri.lazymodel.UserLazyDataModel;
import com.ricchieri.model.Role;
import com.ricchieri.model.User;
import com.ricchieri.service.interf.IDocumentTypeService;
import com.ricchieri.service.interf.IUserService;
import com.ricchieri.util.FacesUtil;
import com.ricchieri.util.StringUtils;

@ManagedBean(name = "searchUserMB")
@ViewScoped
public class SearchUserManageBean extends GenericManageBean {

	private static final long serialVersionUID = -275710803306101330L;
	
	@ManagedProperty(value = "#{UserService}")
	IUserService userService;
	
	@ManagedProperty(value = "#{DocumentTypeService}")
	IDocumentTypeService documentTypeService;

	private UserFilter userFilter;
	private User selectedUser;
	private String blockMotive;
	
	private UserLazyDataModel userLazyDataModel;
	private Map<String,String> documents = null;
	
	@PostConstruct
	public void init() {
		userFilter = (UserFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
		
		if(userFilter == null) {
			userFilter = new UserFilter();
		}
	}

	public UserFilter getUserFilter() {
		return userFilter;
	}

	public void setUserFilter(UserFilter userFilter) {
		this.userFilter = userFilter;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public void search() {
		userLazyDataModel = null;
	}
	
	public void reset() {
		userFilter = new UserFilter();
		userLazyDataModel = null;
	}

	public String viewUser(User user) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_USER, user);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, userFilter);
		return BusinessSettings.VIEW_DETAILS;
	}
	
	public String editUser(User user) {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_USER, user);
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, userFilter);
		return BusinessSettings.EDIT;
	}

	public void showDialog(User user) {
		selectedUser = user;
		blockMotive = user.getBlockMotive();
	}

	public User getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(User selectedUser) {
		this.selectedUser = selectedUser;
	}
	
	public void delete() {
		getUserService().deleteUser(getSelectedUser());
		getUserLazyDataModel().setRowCount(getUserLazyDataModel().getRowCount() -1 );
		FacesUtil.showInfoMessage("searchUserForm", "user_deleted");
	}
	
	public void unauthorize() {
		getUserService().unauthorizeUser(getSelectedUser());
		this.search();
		FacesUtil.showInfoMessage("searchUserForm", "user_unautorized");
	}
	
	public void authorize() {
		getUserService().authorizeUser(getSelectedUser());
		this.search();
		FacesUtil.showInfoMessage("searchUserForm", "user_autorized");
	}
	
	public void unblockUser() {
		getSelectedUser().setBlock(Boolean.FALSE);
		getSelectedUser().setBlockMotive(null);
		getUserService().updateUser(getSelectedUser());
		this.search();
		FacesUtil.showInfoMessage("searchUserForm", "userUnblockedOperations");
	}
	
	public String blockUser() {
		if(StringUtils.isEmpty(blockMotive)) {
			Boolean isValid = Boolean.FALSE;
			FacesUtil.addErrorMsg("searchUserForm:message", "requiredBlockMotive");
			RequestContext context = RequestContext.getCurrentInstance();
			context.addCallbackParam("isValid", isValid);
			
			return BusinessSettings.UNSUCCESS;
		} else {
			getSelectedUser().setBlockMotive(blockMotive);
			getSelectedUser().setBlock(Boolean.TRUE);
			getUserService().updateUser(getSelectedUser());
			this.search();
			FacesUtil.showInfoMessage("searchUserForm", "userBlockedOperations");
			
			return BusinessSettings.SEARCH_USER;
		}
	}
	
	
	public void unselect() {
		this.setSelectedUser(null);
	}

	public UserLazyDataModel getUserLazyDataModel() {
		if(userLazyDataModel == null) {
			userLazyDataModel = new UserLazyDataModel(userService, userFilter);
		}
		return userLazyDataModel;
	}

	public void setUserLazyDataModel(UserLazyDataModel userLazyDataModel) {
		this.userLazyDataModel = userLazyDataModel;
	}
	
	public IDocumentTypeService getDocumentTypeService() {
		return documentTypeService;
	}

	public void setDocumentTypeService(IDocumentTypeService documentTypeService) {
		this.documentTypeService = documentTypeService;
	}
	
	private Boolean showPassengerBlock(User user) {
		for (Role role : user.getActiveRoles()) {
			if(role.getDescription().equals(BusinessSettings.ROLE_PASSANGER)) {
				return Boolean.TRUE;
			}
		}
		return Boolean.FALSE;
	}
	
	public Boolean showUnblock(User user) {
		if(showPassengerBlock(user) && user.getBlock()) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}
	
	public Boolean showBlock(User user) {
		if(showPassengerBlock(user) && !user.getBlock()) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	public String getBlockMotive() {
		return blockMotive;
	}

	public void setBlockMotive(String blockMotive) {
		this.blockMotive = blockMotive;
	}

	public Map<String, String> getDocuments() {
		if(documents == null) {
			documents = this.getDocumentTypeService().getComboDocuments();
		}
		return documents;
	}
}

